package com.demo.controller;



import com.demo.annotation.GlobalInterceptor;
import com.demo.annotation.VerifyParam;
import com.demo.config.AppConfig;
import com.demo.entity.dto.SessionWebUserDto;
import com.demo.entity.enums.ResponseCodeEnum;
import com.demo.entity.po.UserInfo;

import com.demo.entity.query.UserInfoQuery;


import com.demo.entity.vo.ResponseVO;

import com.demo.exception.BusinessException;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import com.demo.service.UserInfoService;


@RestController("userInfoController")
@RequestMapping("/userInfo")
public class UserInfoController extends ABaseController {

	@Resource
	private UserInfoService userInfoService;

	@Resource
	private AppConfig appConfig;



	/**
	 * 分页查询
	 * 按照创建时间降序
	 */
	@RequestMapping("/loadUserList")
	@GlobalInterceptor
	public ResponseVO loadDataList(UserInfoQuery query) {
		query.setOrderBy("create_time desc");
		return getSuccessResponseVO(userInfoService.findListByPage(query));
	}

	/**
	 * 根据UserId查询
	 */
	@RequestMapping("/getUserInfo")
	@GlobalInterceptor(checkParams = true)
	public  ResponseVO getUserByUserId(@VerifyParam(required = true)
										   Long userId) {
		UserInfo userInfoDB = this.userInfoService.getUserInfoByUserId(userId);
		return getSuccessResponseVO(userInfoDB);
	}


	/**
	 * 根据UserId删除
	 */
	@RequestMapping("/deleteUserInfo")
	@GlobalInterceptor(checkParams = true,checkAdmin = true)
	public ResponseVO deleteUserById(@VerifyParam(required = true) Long userId) {
		this.userInfoService.deleteUserInfoByUserId(userId);
		return getSuccessResponseVO(null);
	}


	/**
	 * 保存用户实体
	 * @param bean
	 * @return
	 */
	@RequestMapping("/saveUser")
	@GlobalInterceptor(checkAdmin = true)
	public ResponseVO saveUser(HttpSession session, UserInfo bean){
		this.userInfoService.saveUserInfo(bean);
		return getSuccessResponseVO(null);
	}

	/**
	 * 保存我的信息
	 * @param bean
	 * @return
	 */
	@RequestMapping("/saveMyInfo")
	@GlobalInterceptor
	public ResponseVO saveMyInfo(HttpSession session, UserInfo bean){
		SessionWebUserDto webUserDto = getUserInfoFromSession(session);
		if (!webUserDto.getAdmin()&&bean.getUserId()!=null&&!webUserDto.getUserId().equals(bean.getUserId())){
			throw new BusinessException(ResponseCodeEnum.CODE_600);
		}
		bean.setUserId(webUserDto.getUserId());
		this.userInfoService.saveUserInfo(bean);
		return getSuccessResponseVO(null);
	}


	/**
	 * 修改用户状态
	 * @param userId
	 * @param status
	 * @return
	 */
	@RequestMapping("/updateStatus")
	@GlobalInterceptor(checkParams = true,checkAdmin = true)
	public ResponseVO updateStatus(@VerifyParam(required = true) Long userId,
								   @VerifyParam(required = true) Integer status
	){
		this.userInfoService.updateStatus(userId,status);
		return getSuccessResponseVO(null);
	}

}